package com.whu.tcx01.utils;

public class Transform {
    private static final double M_PI = Math.PI;
    // 经纬度转墨卡托
    // 经度(lon)，纬度(lat)
    public static double[] lonLat2Mercator(double lon,double lat) {
        double[] xy = new double[2];
        double x = lon *20037508.342789/180;
        double y = Math.log(Math.tan((90+lat)*M_PI/360))/(M_PI/180);
        y = y *20037508.34789/180;
        xy[0] = x;
        xy[1] = y;
        return xy;
    }

    //墨卡托转经纬度
    public static double[] mercator2LonLat(double mercatorX,double mercatorY) {
        double[] xy = new double[2];
        double x = mercatorX/20037508.34*180;
        double y = mercatorY/20037508.34*180;
        y= 180/M_PI*(2*Math.atan(Math.exp(y*M_PI/180))-M_PI/2);
        xy[0] = x;
        xy[1] = y;
        return xy;

    }
    public static double lonLatDistance(double lon1,double lat1, double lon2,double lat2){
        double[] start = lonLat2Mercator(lon1, lat1);
        double[] end = lonLat2Mercator(lon2, lat2);
        return mercatorDistance(start[0],start[1],end[0],end[1]);
    }
    public static double mercatorDistance(double mercatorX1,double mercatorY1,double mercatorX2,double mercatorY2){
        return Math.sqrt(Math.pow(mercatorX1-mercatorX2,2)+Math.pow(mercatorY1-mercatorY2,2));
    }
}